import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class FollowedPage extends GetView<FollowedController> {
  const FollowedPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FollowedController>(
      init: FollowedController(),
      id: "follow",
      builder: (_) {
        return Scaffold(
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onRefresh: _.onRefresh,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildBody(physics);
            },
          ),
        );
      },
    );
  }

  Widget _buildBody(ScrollPhysics physics) {
    return GetBuilder<FollowedController>(
      id: 'list',
      builder: (_) {
        return ListView.separated(
          physics: physics,
          itemCount: _.state.list.rows.length,
          separatorBuilder: (BuildContext context, int index) {
            return const SizedBox(height: 1);
          },
          itemBuilder: (BuildContext context, int index) {
            var item = _.state.list.rows[index];
            return Container(
              color: Colors.white,
              padding: const EdgeInsets.symmetric(
                vertical: 10,
                horizontal: 15,
              ),
              child: UserInfoCell(
                name: item.creatorName ?? '',
                avatar: item.creatorAvatar,
                role: item.followTime,
                showNameAuth: item.officialFlag == 1,
                roleFontSize: 13,
                rightChild: GetBuilder<FollowedController>(
                  id: 'btn',
                  builder: (_) => _buildBtn(
                    text: item.toEachFlag == 1 ? '互关'.tr : '回关'.tr,
                    isOutline: item.toEachFlag == 1,
                    onPressed: () => _.onFollowHandle(index),
                  ),
                ),
                onUserTap: () => _.toUserDetailPage(item.customerId),
              ),
            );
          },
        );
      },
    );
  }

  Widget _buildBtn({
    required String text,
    bool isOutline = false,
    VoidCallback? onPressed,
  }) {
    Widget btn;
    if (isOutline) {
      btn = OutlinedButton(
        onPressed: onPressed,
        style: OutlinedButton.styleFrom(
          padding: const EdgeInsets.all(0),
          side: const BorderSide(color: AppColors.danger, width: 1),
          foregroundColor: Colors.white,
          enableFeedback: true,
        ),
        child: Text(
          text,
          style: const TextStyle(color: AppColors.danger, fontSize: 12),
        ),
      );
    } else {
      btn = ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.all(0),
          backgroundColor: AppColors.danger,
          foregroundColor: Colors.white,
          enableFeedback: true,
        ),
        child: Text(
          text,
          style: const TextStyle(color: Colors.white, fontSize: 12),
        ),
      );
    }

    return SizedBox(
      width: 62,
      height: 24,
      child: btn,
    );
  }
}
